package com.course.finalproject.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.IntentService;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v4.content.LocalBroadcastManager;

import com.course.finalproject.Constants;
import com.course.finalproject.model.BatteryLog;

public class BatteryService extends IntentService {

	/**
	 * Constructor.
	 */
	public BatteryService() {
		super("BatteryService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Date now = new Date();
		String date = new SimpleDateFormat("yyyy-MM-dd").format( now );
		String time = new SimpleDateFormat("HH:mm:ss").format( now );
        int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        
        BatteryLog log = new BatteryLog(date, time, rawLevel, scale);
        
        Intent broadcastIntent = new Intent(Constants.NEW_LOG_BATTERY_MSG);
        broadcastIntent.putExtra(Constants.BATTERY_LOG_PARCEL_KEY, log);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
	}

}
